#include <stdio.h>
#include "Camera.h"
#include "GL/glut.h"

Camera::Camera(Mouse * mouse)
{
	this->mouse = mouse;
	size = Vector2D(100,100);
	desiredPosition = Vector2D(0,0);
}
Camera::~Camera()
{
	
}
	
void Camera::ZoomIn()
{
	size = size * 0.95;
}
void Camera::ZoomOut()
{
	size = size * 1.05;
}
Vector2D Camera::GetSize()
{
	return size;
}
Vector2D Camera::GetPosition()
{
	return position;
}
void Camera::Think(double elapsedTime)
{
	HandleMouse();
	Vector2D gap = desiredPosition -position;
	if(gap.Magnitude() > 0)
	{
		position = position + (gap * elapsedTime * 50.0);
	}
}
void Camera::HandleMouse()
{
//if the mouse is near the edge, we need to shift

	
	Vector2D toMouse = mouse->GetPosition();
	#define SHIFT 0.3
	#define BIG_SHIFT 1.0
	Vector2D shift = Vector2D(0,0);
	if(toMouse.x > 0.9 * size.x)
		shift = Vector2D(BIG_SHIFT,0);
	else if(toMouse.x < -0.9 * size.x)
		shift = Vector2D(-BIG_SHIFT,0);
	else if(toMouse.x > 0.8 * size.x)
		shift = Vector2D(SHIFT,0);
	else if(toMouse.x < -0.8 * size.x)
		shift = Vector2D(-SHIFT,0);
		
	if(toMouse.y > 0.9 * size.y)
		shift = Vector2D(0, BIG_SHIFT);
	else if(toMouse.y < -0.9 * size.y)
		shift = Vector2D(0,-BIG_SHIFT);
	else if(toMouse.y > 0.8 * size.y)
		shift = Vector2D(0,SHIFT);
	else if(toMouse.y < -0.8 * size.y)
		shift = Vector2D(0,-SHIFT);
		
	desiredPosition = desiredPosition + shift;	
	mouse->AddShift(shift);
	/*if(toMouse.Magnitude() > 0.6 * (size.Magnitude()))
	{
		desiredPosition = desiredPosition + (!toMouse * 0.5);
	}*/
}
void Camera::Draw()
{
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0,1,0,1, -1, 1);	
	
	glColor3f(1,1,1);
	glMatrixMode(GL_MODELVIEW);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	
	glOrtho(position.x - size.x,position.x + size.x, position.y - size.y, position.y + size.y, -1, 1);	
	glMatrixMode(GL_MODELVIEW);
}
